package com.ruoyi.common.utils;

import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.*;

@SuppressWarnings("unchecked")
public abstract class ObjectUtils {

	public static String percent(int divisor,int dividend){
	   String baifenbi="";
	   double baiy=divisor*1.0;
	   double baiz=dividend*1.0;
	   double fen=baiy/baiz;
	   DecimalFormat df1 = new DecimalFormat("#0.00%");  
	   baifenbi= df1.format(fen);  
	   return baifenbi;
	}
	
	public static <T> List<T> removeDuplicateWithOrder(List<T> list) {
        Set<T> set = new HashSet<T>();
        List<T> newList = new ArrayList<T>();
        for (T e : list) {
            if (set.add(e)){
                newList.add(e);
            }
        }
        return newList;
    }

	
	public static boolean isNull(Object obj) {
		return obj == null;
	}

	public static boolean isNotNull(Object obj) {
		return !isNull(obj);
	}

	public static boolean ObjNotEmptyAndZero(Object obj) {
		if (obj == null)
			return true;
		if ("".equals(obj.toString().trim()) || "0".equals(obj.toString())) {
			return true;
		}
		return false;
	}

	public static String ObjToString(Object obj) {
		return obj == null ? "" : obj.toString();
	}
	
	public static <T> Object[] toObjects(T[] fields){
		List<Object> objs = new ArrayList<Object>();
		for(T t : fields){
			objs.add(t);
		}
		return objs.toArray();
	}

	public static int ObjToInt(Object obj) {
		String strInt = ObjToString(obj);
		if ("".equals(strInt))
			strInt = "0";
		try {
			return new BigDecimal(strInt).intValue();
		}
		catch (Exception e) {
			return 0;
		}
	}

	public static boolean ObjToBoolean(Object obj) {
		String strBoolean = ObjToString(obj);
//		if ("".equals(strBoolean)){
//			strBoolean = "false";
//		}else{
//			if("0".equals(strBoolean) || "n".equalsIgnoreCase(strBoolean) || "false".equalsIgnoreCase(strBoolean)){
//				strBoolean = "false";
//			}else{
//				strBoolean = "true";
//			}
//		}
		if("".equals(strBoolean) || "0".equals(strBoolean) || "n".equalsIgnoreCase(strBoolean) || "false".equalsIgnoreCase(strBoolean)){
			strBoolean = "false";
		}else{
			strBoolean = "true";
		}
		return new Boolean(strBoolean);
	}

	public static String lobToString(Object obj) {
		String str = "";
		if(obj == null){
			return str;
		}
		if (obj instanceof Blob) {
			Blob blob = (Blob) obj;
			try {
				str = new String(blob.getBytes(new Long(1), ObjToInt(blob
						.length())));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if (obj instanceof Clob) {
			Clob clob = (Clob) obj;
			try {
				Reader reader = clob.getCharacterStream();
				char[] chars = new char[1024];
				while (reader.read(chars) != -1) {
					str += new String(chars);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else{
			str = obj.toString();
		}
		return str;
	}

	public static Long ObjToLong(Object obj) {
		if (obj == null || "".equals(obj.toString()))
			return new Long(0);
		else {
			try {
				return ObjToLong(ObjToString(obj));
			} catch (Exception e) {
				e.printStackTrace();
				return new Long(0);
			}
		}
	}

	public static Long ObjToLong(String obj) {
		if (obj == null || "".equals(obj))
			return new Long(0);
		else {
			try {
				return new BigDecimal(obj).longValue();
			} catch (Exception e) {
				e.printStackTrace();
				return new Long(0);
			}
		}
	}
	
	public static Float ObjToFloat(Object obj) {
		if (obj == null || "".equals(obj.toString()))
			return new Float(0.0);
		else {
			try {
				return ObjToFloat(ObjToString(obj));
			} catch (Exception e) {
				e.printStackTrace();
				return new Float(0);
			}
		}
	}
	
	public static Float ObjToFloat(String obj) {
		if (obj == null || "".equals(obj))
			return new Float(0.0);
		else {
			try {
				return new BigDecimal(obj).floatValue();
			} catch (Exception e) {
				e.printStackTrace();
				return new Float(0.0);
			}
		}
	}
	
	public static Double ObjToDouble(Object obj) {
		if (obj == null || "".equals(obj.toString()))
			return new Double(0.0);
		else {
			try {
				return ObjToDouble(ObjToString(obj));
			} catch (Exception e) {
				e.printStackTrace();
				return new Double(0);
			}
		}
	}
	
	public static Double ObjToDouble(String obj) {
		if (obj == null || "".equals(obj))
			return new Double(0.0);
		else {
			try {
				return new BigDecimal(obj).doubleValue();
			} catch (Exception e) {
				e.printStackTrace();
				return new Double(0.0);
			}
		}
	}

	public static boolean isEmpty(Object[] array) {
		return (array == null || array.length == 0);
	}

	public static boolean isEmpty(Collection collection) {
		return (collection == null || collection.isEmpty());
	}

	public static boolean isEmpty(Map map) {
		return (map == null || map.isEmpty());
	}

	public static <T> T[] mapToObjects(Map<String, T> map) {
		Object[] objects = new Object[map.entrySet().size()];
		int i = 0;
		for (Map.Entry<String, T> entry : map.entrySet()) {
			objects[i] = entry.getValue();
			i++;
		}
		return (T[]) objects;
	}

	public static <T> List<T> mapToList(Map<String, T> map) {
		List<T> list = new ArrayList<T>();
		for (Map.Entry<String, T> entry : map.entrySet()) {
			list.add(entry.getValue());
		}
		return list;
	}

	
	public static Map<String, Object> objToMap(Object obj) {
		Map<String, Object> parmas = new HashMap<String, Object>();
		Field[] fields = obj.getClass().getDeclaredFields();
		Field[] fields2 = obj.getClass().getSuperclass().getDeclaredFields();
		fields = ObjectUtils.arraycopy(fields, fields2, Field.class);
		Object valueObject = null;
		for (Field field : fields) {
			if(field.getModifiers()== 25 || field.getModifiers() ==  26){
				continue;
			}
			valueObject = ReflectionUtils.getFieldValue(obj, field
					.getName());
			if (valueObject != null) {
				parmas.put(field.getName(), valueObject);
			}
		}
		return parmas;
	}
	
	public static Map<String, Object> objAllFieldToMap(Object obj) {
		Map<String, Object> parmas = new HashMap<String, Object>();
		Field[] fields = obj.getClass().getDeclaredFields();
		Field[] fields2 = obj.getClass().getSuperclass().getDeclaredFields();
		fields = ObjectUtils.arraycopy(fields, fields2, Field.class);
		Object valueObject = null;
		for (Field field : fields) {
			if(field.getModifiers()== 25 || field.getModifiers() ==  26){
				continue;
			}
			valueObject = ReflectionUtils.getFieldValue(obj, field
					.getName());
			if (valueObject == null) {
				valueObject = "";
			}
			parmas.put(field.getName(), valueObject);
		}
		return parmas;
	}
	public static Object[] entityToArray(Object obj){
		List<Object> params = new ArrayList<Object>();
		Field[] fileds = obj.getClass().getDeclaredFields();
		Object valueObject = null;
		for (Field filed : fileds) {
			valueObject = ReflectionUtils.getFieldValue(obj, filed
					.getName());
			if (valueObject != null) {
				params.add(valueObject);
			}
		}
		return params.toArray();
	}
	
	public static <T> T[] toArray(List list,Class<T> cls){
		T[] t = (T[])java.lang.reflect.Array.newInstance(cls, list.size());
		return (T[])list.toArray(t);
	}
	
	public static <T> List<T> toList(T[] t){
		return Arrays.asList(t);
	}
	
	public static <T> T[] arraycopy(T[] a,T[] b,Class<T> cls){
		T[] t = (T[])java.lang.reflect.Array.newInstance(cls, a.length+b.length);
		System.arraycopy(a, 0, t, 0, a.length);
		System.arraycopy(b, 0, t, a.length, b.length);
		return t;
	}
	
	public static void main(String[] args) {
//		Map<String, Object> filter = new HashMap<String, Object>();
//		filter.put("order", "asc");
//		filter.put("orderBy", "username");
//		filter.put("ces", "ces");
//		System.out.println(StringUtils.ArrayToString(ObjectUtils.mapToObjects(filter)));
//		RemovalEntity obj = new RemovalEntity();
//		obj.setId(new Long(1));
//		System.out.println("obj to [] :"+StringUtils.ArrayToString(ObjectUtils.entityToArray(obj)));
//		
//		List<String> list = new ArrayList<String>();
//		list.add("aa");
//		list.add("aa");
//		list.add("aa");
//		String[] t = ObjectUtils.toArray(list, String.class);
//		System.out.println("t : " +StringUtils.ArrayToString(t));
	}
}